export function auth() {
    let str = [...arguments].join(':');
    return btoa(encodeURIComponent(str));
}

export function getBinaryString(byte) {
    //if (byte < 0) return pad(~byte.toString(2), 8)
    return pad((byte & 0xff).toString(2), 8);
}

export function pad(num, size, sign) {
    var s = Math.abs(num) + '';
    while (s.length < size) s = '0' + s;
    if (num < 0) s = '-' + s;
    else if (sign) s = '+' + s;
    return s;
}

export function getHexString(byte) {
    var hex = Number(byte)
        .toString(16)
        .toUpperCase();
    if (hex.length < 2) hex = '0' + hex;
    return hex;
}

export function checkBit(bitmask, bit) {
    if (!bitmask) return;
    let byteIndex = Math.floor(bit / 8),
        bitIndex = bit - byteIndex * 8;
    bitmask = bitmask.constructor === Array ? bitmask[byteIndex] : bitmask;
    return (bitmask & (1 << bitIndex)) !== 0;
}

export function setBit(bitmask, bit, isOn) {
    let byteIndex = Math.floor(bit / 8),
        bitIndex = bit - byteIndex * 8;
    if (bitmask.constructor !== Array) isOn ? (bitmask |= 1 << bitIndex) : (bitmask &= ~(1 << bitIndex));
    else isOn ? (bitmask[byteIndex] |= 1 << bitIndex) : (bitmask[byteIndex] &= ~(1 << bitIndex));
    return bitmask;
}

export function getNumberFromByteArray(byteArray, startIndex, length) {
    if (!byteArray) return;
    let number = 0,
        byteIndex = startIndex / 8;
    while ((length -= 8) >= 0) {
        number |= (byteArray[byteIndex++] & 0xff) << length;
    }
    return number;
}

export function byteArrayToString(bytes) {
    // needed because TextDecoder is not available in IE11
    let chunkSize = 0x8000,
        chars = [];
    for (let i = 0; i < bytes.length; i += chunkSize) {
        chars.push(String.fromCharCode.apply(null, bytes.subarray(i, i + chunkSize)));
    }
    return chars.join('');
}

/**
 * checkPointInCircle
 * @param ox 原点坐标
 * @param oy
 * @param r 需要判断的半径
 * @param x 需要判断的点坐标
 * @param y
 * @returns boolean
 */
export function checkPointInCircle(ox, oy, r, x, y) {
    return (x - ox) * (x - ox) + (y - oy) * (y - oy) < r * r;
}
export function checkPointInPolyline(point, polylinePoints) {
    // 点击是否在三角形内
    if (polylinePoints.length === 3) {
        return pointIntriangle(
            point.x,
            point.y,
            polylinePoints[0].x,
            polylinePoints[0].y,
            polylinePoints[1].x,
            polylinePoints[1].y,
            polylinePoints[2].x,
            polylinePoints[2].y
        );
    }
    //射线法
    let leftSide = 0;
    const A = point;
    for (let i = 0; i < polylinePoints.length; i++) {
        let B, C;
        if (i === polylinePoints.length - 1) {
            B = {
                x: polylinePoints[i].x,
                y: polylinePoints[i].y
            };
            C = {
                x: polylinePoints[0].x,
                y: polylinePoints[0].y
            };
        } else {
            B = {
                x: polylinePoints[i].x,
                y: polylinePoints[i].y
            };
            C = {
                x: polylinePoints[i + 1].x,
                y: polylinePoints[i + 1].y
            };
        }
        //判断左侧相交
        let sortByY = [B.y, C.y].sort((a, b) => a - b);
        if (sortByY[0] < A.y && sortByY[1] > A.y) {
            if (B.x < A.x || C.x < A.x) {
                leftSide++;
            }
        }
    }
    return leftSide % 2 === 1;
}
// 点击是否在三角形内
export function pointIntriangle(x0, y0, x1, y1, x2, y2, x3, y3) {
    let divisor = (y2 - y3) * (x1 - x3) + (x3 - x2) * (y1 - y3);
    let a = ((y2 - y3) * (x0 - x3) + (x3 - x2) * (y0 - y3)) / divisor,
        b = ((y3 - y1) * (x0 - x3) + (x1 - x3) * (y0 - y3)) / divisor,
        c = 1 - a - b;
    return a >= 0 && a <= 1 && b >= 0 && b <= 1 && c >= 0 && c <= 1;
}
export function round22(value) {
    return parseFloat(Math.round(value * 100) / 100).toFixed(2);
}
